package xyz.jangle.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOMain {

	public static void main(String[] args) {
//		test();

		try (InputStream in = new FileInputStream(new File("D:/桌面/huhongjing.txt"));
				FileOutputStream out = new FileOutputStream(new File("D:/桌面/huhongjing2.txt"));) {
			byte[] buff = new byte[128];
			int length = 0;
//			StringBuffer sb = new StringBuffer();
			while ((length = in.read(buff)) != -1) {
				out.write(buff, 0, length);
//				System.out.println(new String(buff,0,length));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	// 先写后读
	@SuppressWarnings("unused")
	private static void test() {
		try {
			byte bWrite[] = { 65, 66, 67, 68, 5 };
			OutputStream os = new FileOutputStream("test.txt");
			for (int x = 0; x < bWrite.length; x++) {
				os.write(bWrite[x]); // writes the bytes
			}
			os.close();

			InputStream is = new FileInputStream("test.txt");
			int size = is.available();

			for (int i = 0; i < size; i++) {
				System.out.print((char) is.read() + "  ");
			}
			is.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
